/*
 * @(#) $Header$
 *
 * Copyright (C) 2007  Daniel Léonard
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */

package ca.forklabs.media.jai;

import java.awt.Point;
import java.awt.image.BandedSampleModel;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferByte;
import java.awt.image.DataBufferDouble;
import java.awt.image.DataBufferFloat;
import java.awt.image.DataBufferInt;
import java.awt.image.DataBufferShort;
import java.awt.image.DataBufferUShort;
import java.awt.image.Raster;
import java.awt.image.SampleModel;
import java.awt.image.WritableRaster;
import javax.media.jai.ImageLayout;
import javax.media.jai.PlanarImage;
import ca.forklabs.media.jai.Resources;

/**
 * Class {@code RasterAdapter} is a {@link PlanarImage} adapter over a
 * {@link WritableRaster}. The resulting image has only one tile and the
 * {@link ImageLayout} information, except for the {@link ColorModel} that is
 * taken directly from the raster.
 *
 * @author   <a href="mailto:forklabs at gmail.com?subject=ca.forklabs.media.jai.RasterAdapter">Daniel Léonard</a>
 * @version $Revision$
 */
@SuppressWarnings("unchecked")
public class RasterAdapter extends PlanarImage {

//---------------------------
// Instance variable
//---------------------------

   /** The raster. */
   private WritableRaster writable_raster;


//---------------------------
// Constructor
//---------------------------

   /**
    * Constructor.
    * @param   raster   the underlying raster.
    * @exception   NullPointerException   if {@code raster} is {@code null}.
    */
   public RasterAdapter(WritableRaster raster) {
      ImageLayout layout = this.setUpImageLayout(raster);
      this.setImageLayout(layout);
      this.setWritableRaster(raster);
      }


//---------------------------
// Accessor and mutator
//---------------------------

   /**
    * Gets the raster.
    * @return   the raster.
    */
   protected WritableRaster getWritableRaster() {
      return this.writable_raster;
      }

   /**
    * Changes the raster.
    * @param   raster   the new raster.
    */
   protected void setWritableRaster(WritableRaster raster) {
      this.writable_raster = raster;
      }


//---------------------------
// Implemented method from javax.media.jai.PlanarImage
//---------------------------

   /**
    * Returns the underlying raster (as a {@link WritableRaster}). There is only
    * one tile (0, 0).
    * @param   x   the <em>x</em> position of the tile.
    * @param   y   the <em>y</em> position of the tile.
    * @return   the tile.
    * @exception   IndexOutOfBoundsException   if the requested tile is not
    *                                          <em>(0, 0)</em>.
    */
   @Override
   public Raster getTile(int x, int y) throws IndexOutOfBoundsException {
      if (0 != x || 0 != y) {
         String message = RasterAdapter.getBadTileErrorMessage(x, y);
         throw new IndexOutOfBoundsException(message);
         }
      Raster raster = this.getWritableRaster();
      return raster;
      }


//---------------------------
// Instance methods
//---------------------------

   /**
    * Sets up an {@link ImageLayout} with information extracted from the given
    * {@link Raster}.
    * @param   raster   the raster behing this image.
    * @return   the new image layout.
    */
   protected ImageLayout setUpImageLayout(Raster raster) {
// TODO : set the tile properties so that there be only one tile ?
      ImageLayout layout = new ImageLayout();

      layout.setMinX(raster.getMinX())
            .setMinY(raster.getMinY())
            .setWidth(raster.getWidth())
            .setHeight(raster.getHeight())
            .setSampleModel(raster.getSampleModel());

      return layout;
      }


//---------------------------
// Class methods
//---------------------------

   /**
    * Builds an image from a {@link DataBuffer}.
    * @param   buffer   the data buffer.
    * @param   cols   the number of columns.
    * @param   rows   the number of rows.
    * @return   a new image.
    */
   public static RasterAdapter buildImage(DataBuffer buffer, int cols, int rows) {
      int type = buffer.getDataType();
      int bands = buffer.getNumBanks();
      SampleModel sample_model = new BandedSampleModel(type, cols, rows, bands);
      Point origin = new Point(0, 0);
      WritableRaster raster = Raster.createWritableRaster(sample_model, buffer, origin);
      RasterAdapter image = new RasterAdapter(raster);
      return image;
      }

   /**
    * Builds an image from byte data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferByte#DataBufferByte(byte[][], int)
    */
   public static RasterAdapter buildByteImage(byte[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferByte(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Builds an image from short data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferShort#DataBufferShort(short[][], int)
    */
   public static RasterAdapter buildShortImage(short[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferShort(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Builds an image from unsigned short data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferUShort#DataBufferUShort(short[][], int)
    */
   public static RasterAdapter buildUShortImage(short[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferUShort(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Builds an image from int data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferInt#DataBufferInt(int[][], int)
    */
   public static RasterAdapter buildIntImage(int[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferInt(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Builds an image from float data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferFloat#DataBufferFloat(float[][], int)
    */
   public static RasterAdapter buildFloatImage(float[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferFloat(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Builds an image from double data.
    * @param   pixels   the pixel data.
    * @param   cols   the number of columns in the image.
    * @param   rows   the number of rows in the image.
    * @return   a new image.
    * @see   DataBufferDouble#DataBufferDouble(double[][], int)
    */
   public static RasterAdapter buildDoubleImage(double[][] pixels, int cols, int rows) {
      int size = cols * rows;
      DataBuffer buffer = new DataBufferDouble(pixels, size);
      RasterAdapter image = RasterAdapter.buildImage(buffer, cols, rows);
      return image;
      }

   /**
    * Gets the formatted error message for a request to an inexistant tile.
    * @param   x   the <em>x</em> coordinate of the tile.
    * @param   y   the <em>y</em> coordinate of the tile.
    * @return   the message.
    */
   @SuppressWarnings("boxing")
   protected static String getBadTileErrorMessage(int x, int y) {
      String key = Resources.RASTER_ADAPTER_BAD_TILE;
      String message = Resources.getLocalizedString(key, x, y);
      return message;
      }

   }
